#!/bin/bash

#PPTP VPN Users File w/ Timestamps
credFile="/etc/ppp/chap-secrets"
credFileMirror="/etc/ppp/chap-secrets.mirror"
currentDate=$(date +%s)
cutOffDate=$(expr $currentDate - 43200);

sudo cp $credFile $credFileMirror
sudo sed -i '/jstrusz/d' $credFileMirror

#This can just occur periodically to mark in MySQL as disabled. There is a timestamp flag somehwere i thought...
echo "update sessions set status = 'disabled' WHERE stop_time <= (UNIX_TIMESTAMP(NOW()) - 43200);" | mysql -u root -p3lch@rr099 shellicio_us


#Need to find expired accounts and remove system wide.
echo "Cutoff Date: $cutOffDate"

while read line; do
	username=$(echo $line | cut -d ' ' -f1)
	expireDate=$(echo $line | cut -d ':' -f2)
	if [[ "$expireDate" < "$cutOffDate" ]]; then
		echo "Username: $username, expired on $expireDate. Removing..."
		sudo usedel $username
		sudo rm -rf /home/$username
		echo "drop database db_$username; DROP USER 'db_$username'@'localhost';" | mysql -u root -p3lch@rr099 shellicio_us
		sudo sed -i /"$username"/d $credFile
		sudo sed -i /"$username"/d $credFileMirror
		sudo rm -rf /web/shellicio.us/creds/$username.txt
		echo "Done removing user from system."
	else
		echo "Username: $username, expires on $expireDate."
	fi
done < $credFileMirror

